<?php

class Gadaian_model extends Model {

	var $data;
	
	function Gadaian_model()
	{
		parent::Model();
	}
	
	function set_anggota_id($anggota_id)
	{
		$this->db->where('gadaian.anggota_id', $anggota_id);
	}

	function get_all_data(){
		$this->db->select('gadaian.*, anggota.nama_lengkap as nama_anggota, SUM(transaksi_gadaian.biaya) as saldo_gadaian');
		$this->db->from('gadaian');
		$this->db->join('anggota', 'gadaian.anggota_id=anggota.id_anggota', 'INNER');
		//$this->db->join('jenis_gadaian', 'gadaian.jenis_gadaian=jenis_gadaian.id', 'INNER');
		$this->db->join('transaksi_gadaian', 'gadaian.id=transaksi_gadaian.no_rekening_id', 'left');
		$this->db->group_by('gadaian.id');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		else
		{
			return FALSE;
		}
	}
	
	
	function get_last_id(){
		$query = $this->db->query('select *, CONCAT(SUBSTR(no_rekening, 8, 2),SUBSTR(no_rekening, 5, 2),SUBSTR(no_rekening, 11, 4)) as urut from gadaian order by urut desc limit 1 ');
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}
		else
		{
			return FALSE;
		}
	}

	function get_data_by_id($id){
		$this->db->select('gadaian.id, gadaian.no_rekening, gadaian.anggota_id, anggota.nama_lengkap as nama_anggota, gadaian.spk_no, gadaian.tgl_reg, gadaian.biaya, gadaian.tgl_mulai, gadaian.tgl_selesai, gadaian.keterangan');
		$this->db->from('gadaian');
		$this->db->join('anggota', 'gadaian.anggota_id=anggota.id_anggota', 'INNER');
		$this->db->where('gadaian.id', $id);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			return FALSE;
		}
	}


	function fill_data()
	{
//		echo 'hoho';
//		echo $this->input->post('anggotaID');
		//$status = ($this->input->post('status')) ? $this->input->post('status') : 'Start';
		
		$biaya = $this->input->post('biaya');
		$biaya = str_replace(",00", "", $biaya);
		$biaya = str_replace("Rp", "", $biaya);
		$biaya = str_replace(" ", "", $biaya);
		$biaya = str_replace(".", "", $biaya);
		
		$this->data = array(
			'no_rekening' => $this->input->post('no_rekening'),
			//'anggota_id' => $this->input->post('anggotaID'),
			'anggota_id' => $this->input->post('anggota'),
			'tgl_reg' => $this->input->post('tgl_reg'),
			'biaya' => $biaya,
			'keterangan' => $this->input->post('keterangan')
		);
	}
	
	//Check for duplicate login ID
	function check_name($id = '')
	{
		$this->db->where('no_rekening', $this->data['no_rekening']);
		if($id != '') $this->db->where('id !=', $id);
		$query = $this->db->get('gadaian');

		if ($query->num_rows() > 0)
		{
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}	

	function insert_data()
	{
		$insert = $this->db->insert('gadaian', $this->data);
		return $insert;
	}

	function update_data($id)
	{
		$this->db->where('id', $id);
		$update = $this->db->update('gadaian', $this->data);
		return $update;
	}

	function delete_data($id){
		$this->db->where('id', $id);
		$delete = $this->db->delete('gadaian');
		return $delete;
	}
	

}

/* End of file gadaian_model.php */
/* Location: ./application/models/gadaian_model.php */



